package com.example.mybatisplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.common.utls.SessionUtils;
import com.example.mybatisplus.mapper.ReportupMapper;
import com.example.mybatisplus.model.domain.*;
import com.example.mybatisplus.mapper.ReportMapper;
import com.example.mybatisplus.model.dto.PageDTO;
import com.example.mybatisplus.service.AuditHistoryService;
import com.example.mybatisplus.service.ReportService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mybatisplus.service.ReportupService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 工作上报 服务实现类
 * </p>
 *
 * @author lxp
 * @since 2022-12-28
 */
@Service
public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report> implements ReportService {

    @Autowired
    private ReportMapper reportMapper;


    @Autowired
    private ReportupMapper reportupMapper;


    @Autowired
    private ReportService reportService;


    @Autowired
    private ReportupService reportupService;

    @Autowired
    private AuditHistoryService auditHistoryService;

    //上传
    @Override
    public void rep(Report report) {
        // 填充没有的数据
        // 获取当前登录用户
        Userlist curUser = SessionUtils.getCurUser();
        // 名字   民兵id    师   团   营   连   分队
        report.setName(curUser.getName());
        report.setUserId(curUser.getId());
        report.setShi(curUser.getDivision());
        report.setTuan(curUser.getRegiment());
        report.setYing(curUser.getBattalion());
        report.setLian(curUser.getCompany());
        report.setFendui(curUser.getUnit());
        report.setReplay("待审核");
    }

    //图片路径
    @Override
    public void insert(Report report) {
        //插入一条数据会自动把插入后的id返回
        reportMapper.insert(report);
        List<Reportup> reportups = report.getPaths().stream().map(s ->
                new Reportup().setPath(s).setReportId(report.getId()))
                .collect(Collectors.toList());
        reportupService.saveBatch(reportups);
    }

    @Override
    public Page<Report> listPage(Report report, PageDTO pageDTO) {
        Page<Report> page = new Page<>(pageDTO.getPageNo(), pageDTO.getPageSize());
        QueryWrapper<Report> wrapper = new QueryWrapper<>();
        reportMapper.selectPage(page, wrapper);

        for (Report report1 : page.getRecords()) {
            QueryWrapper<Reportup> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("report_id", report.getId());

            QueryWrapper<Report> wrapper2 = new QueryWrapper<>();
            List<Report> reports = reportService.list(wrapper2);

            Reportup reportup = reportupMapper.selectOne(wrapper1);
            if (reportup != null) {
                report1.setPath(reportup.getPath());
                report1.setReports(reports);
            }
        }
        return page;
    }

    @Override
    public List<Report> apply() {
        Userlist curUser = SessionUtils.getCurUser();
        QueryWrapper<Report> wrapper = new QueryWrapper<>();
        wrapper.eq("name", curUser.getName());
        return reportMapper.selectList(wrapper);
    }

    //待我审核
    @Override
    public void audit(Report report) {
        Report report1 = reportMapper.selectById(report.getId());
        Userlist curUser = SessionUtils.getCurUser();
        AuditHistory auditHistory = new AuditHistory()
                .setReportId(report.getUserId())
                .setAuditId(curUser.getId())
                .setAuditorName(curUser.getName())
                .setAuditComment(report.getSuggest())
                .setReplay(report.getReplay())
                .setName(report.getName());
        auditHistoryService.save(auditHistory);
        if (report.getReplay().equals("不通过")) {
            report1.setSuggest(report.getSuggest()).setReplay(report.getReplay());
        } else {
            report1.setReplay(report.getReplay());
        }
        reportMapper.updateById(report1);
    }


}
